home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / GX Libraries / PicturesAndPICTLibrary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-16  |  5.3 KB  |  196 lines  |  [TEXT/MPS ]

  1.  
  2. /*
  3.     File:        PicturesAndPICTLibrary.c
  4.  
  5.     Contains:    This file contains sample routines for dealing with pictures
  6.                 within PICTs.
  7.  
  8.     Version:    Quickdraw GX 1.1
  9.  
  10.     Written by:    Dave Hersey
  11.  
  12.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  13.  
  14.     File Ownership:
  15.  
  16.         DRI:                      Dave Hersey
  17.  
  18.         Other Contact:            Ron Voss
  19.  
  20.         Technology:             QuickDraw GX
  21.   
  22.     Change History (most recent first):
  23.  
  24.          <1>      6/6/95    DH        First checked in.
  25.  
  26.     Routines in this file:
  27.         PictureToPICT         - creates a PICT with an embedded picture shape
  28. */
  29.  
  30. #include "PicturesAndPICTLibrary.h"
  31.  
  32. //-----------------------------------------------------------------------------
  33. // INTERNAL TYPEDEFS AND DEFINES
  34. //-----------------------------------------------------------------------------
  35.  
  36. // IDs for the PicComments
  37. #define shapeSignature    'qdgx'
  38. #define shapeBegin        500
  39. #define shapeEnd        501
  40.  
  41. // typedefs for the new PicComments
  42. typedef struct 
  43.     {
  44.     OSType        signature;        // always == shapeSignature
  45.     short        kind;            // always == shapeBegin
  46.     Rect        bounds;            // bounds of shape @ 72 dpi
  47.     char        data[1];        // flattened shape data, total size of record determines
  48.                                 // size
  49.     } ShapeBeginRecord, *ShapeBeginPtr, **ShapeBeginHdl;
  50.     
  51. typedef struct
  52.     {
  53.     OSType        signature;        // always == shapeSignature
  54.     short        kind;            // always == shapeEnd
  55.     } ShapeEndRecord, *ShapeEndPtr, **ShapeEndHdl;
  56.  
  57. //-----------------------------------------------------------------------------
  58.  
  59. PicHandle    PictureToPICT(gxShape theShape, Boolean simpleProxy)
  60. /*
  61.     This library routine turns a QuickDraw GX™ shape into a QuickDraw PICT.
  62.     It does this using three steps:
  63.         1)  it emits a QuickDraw PicComment, and places the flattened GX data into
  64.             the comment.
  65.         2)    it converts the GX picture into a QuickDraw proxy.  It uses a rectangle
  66.             with an 'X' through it if simpleProxy is true.  Otherwise, it uses a 1 bit
  67.             bitmap rendition of the shape.
  68.         3)    it emits a QuickDraw PictComment, marking the end of the QuickDraw proxy.
  69.         
  70.     The resulting PICT can be cut & pasted, or saved into a PICT file.  On a system
  71.     with QuickDraw GX™ installed, the GX data will be used when printing.  
  72.     When printing on other systems, the QuickDraw proxy will be used.
  73.     
  74.     The proxy is always used for display from within non-GX applications.
  75. */
  76. {
  77.     gxGraphicsError        gxErr = noErr;
  78.     PicHandle            thePicture;
  79.     Rect                picRect;
  80.     ShapeBeginRecord    theBegin;
  81.     Handle                hBegin;
  82.     gxRectangle            shapeBounds;
  83.         
  84.     // get the location of the shape
  85.     GXGetShapeDeviceBounds(theShape, 0, 0, &shapeBounds);    
  86.     picRect.left     = FixedToInt(shapeBounds.left);
  87.     picRect.top     = FixedToInt(shapeBounds.top);
  88.     picRect.right     = FixedToInt(shapeBounds.right);
  89.     picRect.bottom     = FixedToInt(shapeBounds.bottom);
  90.     GlobalToLocal((Point*) &picRect.top);
  91.     GlobalToLocal((Point*) &picRect.bottom);
  92.         
  93.     thePicture = OpenPicture(&picRect);
  94.     if (thePicture != nil)
  95.         {
  96.         // use a standard clipping and pen mode
  97.         ClipRect(&picRect);
  98.         PenNormal();
  99.         
  100.         // flatten our shape out into a handle
  101.         hBegin = ShapeToHandle(theShape);
  102.         if (hBegin != nil)
  103.             {
  104.             // add the comment to the begining of the handle
  105.             theBegin.signature     = shapeSignature;
  106.             theBegin.kind         = shapeBegin;
  107.             theBegin.bounds     = picRect;
  108.             Munger(hBegin, 0, nil, 0, &theBegin, sizeof(OSType) + sizeof(short) + sizeof(Rect) );
  109.             gxErr = MemError();
  110.         
  111.             // store the shape/handle into the picture
  112.             PicComment(shapeBegin, GetHandleSize(hBegin), hBegin);
  113.             DisposeHandle(hBegin);
  114.             
  115.             // Send the dimensions of the shape at 72 dpi
  116.             PenMode(23);
  117.             FrameRect(&picRect);
  118.             PenNormal();
  119.             }
  120.         else
  121.             gxErr = MemError();
  122.     
  123.         if (gxErr == noErr)
  124.             {
  125.             if (simpleProxy)
  126.                 {
  127.                 // our proxy is just a framed rect with an X through it
  128.                 FrameRect(&picRect);
  129.                 MoveTo(picRect.left,     picRect.top);
  130.                 LineTo(picRect.right,     picRect.bottom);
  131.                 MoveTo(picRect.right,     picRect.top);
  132.                 LineTo(picRect.left,     picRect.bottom);
  133.                 }
  134.             else
  135.                 {
  136.                 // our proxy is a bitmap of the GX object
  137.                 gxBitmap        theBits;
  138.                 gxShape            theBitmap;
  139.  
  140.                 theBits.width         = picRect.right - picRect.left;
  141.                 theBits.height         = picRect.bottom - picRect.top;
  142.                 theBits.pixelSize     = 1;
  143.                 theBits.rowBytes    = ((theBits.width + 31) >> 5) << 2;
  144.                 theBits.image         = NewPtrClear(theBits.height * theBits.rowBytes);
  145.                 theBits.space         = gxIndexedSpace;
  146.                 theBits.set         = nil;
  147.                 theBits.profile     = nil;
  148.                 
  149.                 gxErr = MemError();
  150.                 if (gxErr == noErr)
  151.                     {
  152.                     theBitmap = GXNewBitmap(&theBits, nil);
  153.                     GXMoveTransformTo(GXGetShapeTransform(theBitmap), -shapeBounds.left, -shapeBounds.top);
  154.                     GXGetGraphicsError(&gxErr);
  155.                     if (gxErr == noErr)
  156.                         {
  157.                         BitMap        qdBits;
  158.                         
  159.                         // put the shape into the bitmap
  160.                         CopyToBitmaps(theBitmap, theShape);
  161.                         ConvertToQDBitmap(&theBits, &qdBits);
  162.                         GXGetGraphicsError(&gxErr);
  163.                                                                         
  164.                         // now done with the bitmap
  165.                         GXDisposeShape(theBitmap);
  166.  
  167.                         // CopyBits it into the picture
  168.                         CopyBits(&qdBits, &qd.thePort->portBits, &qdBits.bounds, &picRect, srcOr, nil);
  169.                         
  170.                         }
  171.                         
  172.                     // and done with the image
  173.                     DisposePtr(theBits.image);
  174.                     }
  175.                 }
  176.             
  177.             // mark the end of our shape's proxy
  178.             PicComment(shapeEnd, 0, (Handle) nil);
  179.             }
  180.         
  181.         ClosePicture();
  182.         }
  183.     else
  184.         gxErr = MemError();
  185.         
  186.     // if we had a problem, return a nil picture
  187.     if ( ( gxErr != noErr ) && thePicture )
  188.         {
  189.         KillPicture(thePicture);
  190.         thePicture = nil;
  191.         }
  192.         
  193.     return(thePicture);
  194.     
  195. } // PictureToPICT
  196.